<?php

namespace app\post\controllers;

use mini\upload\Upload;
use mini\Image;
use Exception;

class Up
{
    public function index()
    {
        // dump($_FILES);

        view('post/up');
    }

    public function hi()
    {
        $data = [
            [
                'user_id' => 1,
                'post_id' => 1,
                'created_at' => 1702350916,
            ],
            [
                'user_id' => 2,
                'post_id' => 2,
                'created_at' => 1702350916,
            ],
        ];

        $like = db('likes');
        $result = $like->batchInsert($data);
        $rowCount = $like->rowCount();

        dump($result);
    }

    public function demo()
    {
        $form_field = post('files');
        $upload = new Upload(config('upload'));
        $uploadedFileData = $upload->upload($form_field);

        if ( !$uploadedFileData ) {
            throw new Exception($upload->getError());
        }

        $postId = 3;

        // dd( $uploadedFileData);

        // 准备附件数据，包括文件名、类型和关联信息
        $attachmentsData = array_map(function ($fileData) use ($postId) {
            return [
                'filename'   => $fileData['path'],
                'type'       => $fileData['ext'],
                'post_id'    => $postId,            // 直接在映射时添加 post_id
                'user_id'    => session('user_id'),
                'created_at' => time(),
            ];
        }, $uploadedFileData);

        if (!is_array($attachmentsData)) {
            $attachmentsData = [$attachmentsData];
        }

        dump($attachmentsData);
        die();

        // foreach ($attachmentsData as $filePath) {
        //     echo $filePath['filename'];
        // }

        // die();

        $wideImages = [];

        // 遍历数组中的每个元素
        foreach ($attachmentsData as $image) {
            // 使用getimagesize()函数获取图片的尺寸信息
            list($width, $height, $type, $attr) = getimagesize($image['filename']);

            // 检查宽度是否大于500
            if ($width > 500) {
                // 如果是，则将filename值存储到新的数组中
                $wideImages[] = $image['filename'];
            }
        }



        dump($wideImages);
        die();


        foreach ($wideImages as $img) {
            $image = new Image();

            if ($image->load($img)) {
                $path_parts = pathinfo($img);

                $thumbnailPath = $path_parts['dirname'] . '/' . $path_parts['filename'] . '_thumb.' . $path_parts['extension'];

                $imgWidth = getimagesize($img);

                dd($imgWidth);

                // 调整图像大小
                $image->resizeToWidth(300);
                $image->save($thumbnailPath, $path_parts['extension']);
            }
        }






        // dd($_FILES['files']['name']);
        // $form_field = post('files');
        // $upload = new Upload(config('upload'));
        // $upload_result = $upload->upload($form_field);

        // dd($upload_result);

        // if ($upload_result) {
        //     // 提取需要我键值对，并改名
        //     $data = array_map(function ($item) {
        //         return [
        //             'filename' => $item['path'],
        //             'type'     => $item['ext'],
        //         ];
        //     }, $upload_result);

        //     $arrayToAdd = [
        //         'user_id' => 1,
        //         'post_id' => 1,
        //         'created_at' => 1702350916,
        //     ];

        //     foreach ($data as &$subArray) {
        //         $subArray = array_merge($subArray, $arrayToAdd);
        //     }
        //     unset($subArray); // 取消引用，避免后续意外修改

        //     dd($data);
        // }






        // $merge_data = $result + $data;

        // dd($result);

        // if (count($files) > 1) {
        //     foreach ($files as $k => $v) {
        //         # code...
        //     }
        // }

        // if ($result) {
        //     dump($result);
        // } else {
        //     json($upload->getError());
        // }


    }
}